package com.wgj.android.ui.activity;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.accesslibility.config.WXQiangHongBaoConfig;

import com.wgj.android.accesslibility.service.WXQHBNotificationService;
import com.wgj.android.entity.Version;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.DownloadAppUtils;
import com.wgj.android.utils.TextViweUtils;
import com.wgj.android.utils.fonat.IconFonat;
import com.wgj.android.widget.NumberEditView;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/11/14
 * 描    述：  微信红包助手配制界面
 * 修订历吏：
 * =========================================
 */
public class WXHongBaoConfigActivity extends AppCompatActivity {
    @BindView(R.id.QHB_EY_WECHAT_DELAY_TIME)
    NumberEditView numberEditView;
    @BindView(R.id.QHB_KEY_NOTIFICATION_SERVICE_ENABLE)
    SwitchCompat notificationSwitch;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.wx_qinghongbao_config_activity);
        ButterKnife.bind(this);

        numberEditView.setNumberListener(new NumberEditView.NumberListener() {
            @Override
            public void onValueChanged(int value) {
                 WXQiangHongBaoConfig.setWechatOpenDelayTime(value);
            }
        });
        ((SwitchCompat) findViewById(R.id.QHB_KEY_NOTIFY_NIGHT_ENABLE)).setChecked(WXQiangHongBaoConfig.isNotifyNight());

        ((SwitchCompat) findViewById(R.id.QHB_KEY_NOTIFY_SOUND)).setChecked(WXQiangHongBaoConfig.isNotifySound());
        ((SwitchCompat) findViewById(R.id.QHB_KEY_NOTIFY_VIBRATE)).setChecked(WXQiangHongBaoConfig.isNotifyVibrate());
        ((SwitchCompat) findViewById(R.id.WX_AFTER_GET_GOHOME)).setChecked(WXQiangHongBaoConfig.getWechatAfterOpenHongBao());
        numberEditView.setValue(WXQiangHongBaoConfig.getWechatOpenDelayTime()/1000);
       findViewById(R.id.QHB_KEY_NOTIFICATION_SERVICE_ENABLE).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               SwitchCompat switchCompat= (SwitchCompat) v;
               if(((SwitchCompat) v).isChecked()){
                   new XAlertMsgDialog(WXHongBaoConfigActivity.this,"极速抢红包功能需要开户通知栏服务,是否需要开启").setPositiveButton("开启").setListener(new XAlertMsgDialog.OnCloseListener() {
                       @Override
                       public void onClick(Dialog dialog, boolean b) {
                           if(b){
                               openNotificationServiceSettings();
                           }else {
                               notificationSwitch.setChecked(false);
                           }
                       }
                   }).show();

               }else {
                   new XAlertMsgDialog(WXHongBaoConfigActivity.this,"是否关闭极速抢红包功能").setPositiveButton("关闭").setListener(new XAlertMsgDialog.OnCloseListener() {
                       @Override
                       public void onClick(Dialog dialog, boolean b) {
                           if(b){
                               openNotificationServiceSettings();
                           }else {
                               notificationSwitch.setChecked(true);
                           }
                       }
                   }).show();
               }
           }
       });

    }




    @OnCheckedChanged({R.id.QHB_KEY_NOTIFY_NIGHT_ENABLE,R.id.QHB_KEY_NOTIFY_SOUND,R.id.QHB_KEY_NOTIFY_VIBRATE,R.id.WX_AFTER_GET_GOHOME})
    public   void  onConfigChanged(CompoundButton view, boolean changed ){
        switch (view.getId()){
            case  R.id.QHB_KEY_NOTIFY_NIGHT_ENABLE:
                WXQiangHongBaoConfig.setIsNotifyNight(changed);
                break;
            case R.id.QHB_KEY_NOTIFY_SOUND:
                WXQiangHongBaoConfig.setIsNotifySound(changed);
                break;
            case R.id.QHB_KEY_NOTIFY_VIBRATE:
                WXQiangHongBaoConfig.setIsNotifyVibrate(changed);
                break;
            case R.id.WX_AFTER_GET_GOHOME:
                WXQiangHongBaoConfig.setWechatAfterOpenHongBao(changed);
                break;

        }
    }
    /** 打开通知栏设置*/
    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    private void openNotificationServiceSettings() {
        try {
            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
            startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   public boolean isOpenNotificationReadPermission() {
       Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(this);
       if (packageNames.contains(getPackageName())) {
           return true;
       }
       return false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(WXQHBNotificationService.isRunning()){
            notificationSwitch.setChecked(true);
        }else {
            notificationSwitch.setChecked(false);
         }
    }
}
